﻿import QtQuick 2.10
import QtQuick.Controls 1.4
//import QtQuick.Dialogs 1.2
import QtQuick.Layouts 1.2
import QtDream.Core 3.2
import QtDream.Controls 1.9
import QtDream.Notepad 0.2
import QtQuick.Dialogs 1.2 as MyDialogs


Page
{
    id: page
    implicitWidth: 270
    implicitHeight: 480

    Component.onCompleted: {
        textEdit.focus=true
    }

    property string title: "无标题"
    property string filePath: ""
    property bool needOpen: false       //标记是否需要打开

    //菜单
    menuBar: MenuBar
    {
        Menu
        {
            title: qsTr( "文件" ) + Translator.notifier

            MenuItem {
                text: "新建(N)"
                shortcut: ("Ctrl+N")
                onTriggered: {
                    console.log("click " + text)
                    funcNew()
                }
            }
            MenuItem {
                text: "打开(O)"
                shortcut: ("Ctrl+O")
                onTriggered: {
                    console.log("click " + text)
                    needOpen = true
                    funcOpen()
                }
            }
            MenuItem {
                text: "保存(S)"
                shortcut: ("Ctrl+S")
                onTriggered: {
                    console.log("click " + text)
                    funcSave()
                }
            }
            MenuItem {
                text: "另存为(A)"
                shortcut: ("Ctrl+A")
                onTriggered: {
                    console.log("click " + text)
                    funcSaveAs()
                }
            }
        }
    }

    function funcNew(){
        if(page.title != "无标题"){
            messageDialogNew.open()

        }
        else{
            if(textEdit.text != ""){
                //提示保存
                messageDialogNew.open()
            }
            else{

            }
        }

    }

    function funcOpen(){
        if(page.title == "无标题"){
            messageDialogNewOpen.open()
        }
        else{
            funcWriteFile(textEdit.text, page.filePath)
            fileDialogOpen.open()
        }
    }

    function funcSave(){
        console.log(page.title)
        if(page.title == "无标题"){
            fileDialogSave.open()
        }
        else{
            funcWriteFile(textEdit.text, page.filePath)
        }
    }

    function funcSaveAs(){
        console.log("funcSaveAs")
        fileDialogSaveAs.open()
    }

    function funcWriteFile(text, path){
        NotepadIO.writeFile( text, path );
    }
    function funcWriteFileStr(text, path){
        NotepadIO.writeFileStr( text, path );
    }

    function funcReadFile(path){
        return NotepadIO.readFile(path)
    }


    //主界面
    Item{
        id: item
        width: parent.width
        height: parent.height-y
        y: 22

        //背景 纯色
        Rectangle{
            anchors.fill: parent
            color: "white"
            opacity: 1
        }

        ScrollView{
            id: scrollView
            anchors.fill: parent

            TextEdit{
                id: textEdit
                width: scrollView.width-25
                selectByMouse: true
                focus: true
            }
        }
    }

    //提示框
    MyDialogs.MessageDialog{
        id: messageDialogNew
        visible: false
        title: "记事本"
        text: "是否将更改保存到 " + page.title

        standardButtons: MyDialogs.StandardButton.Save | MyDialogs.StandardButton.Discard | MyDialogs.StandardButton.Cancel
        onAccepted: {
            console.log("save")
            if(page.title == "无标题"){
                fileDialog.open()
            }
            else{
                funcWriteFile(textEdit.text, fileDialog.fileUrl)

                textEdit.text = ""
                page.title = "无标题"
            }
        }
        onDiscard: {
            console.log("Discard")
            page.title = "无标题"
            page.filePath = ""
            textEdit.text = ""

//            fileDialog.open()
        }
        onRejected: {
            console.log("Rejected")
        }
    }

    MyDialogs.MessageDialog{
        id: messageDialogNewOpen
        visible: false
        title: "记事本"
        text: "是否将更改保存到 " + page.title

        standardButtons: MyDialogs.StandardButton.Save | MyDialogs.StandardButton.Discard | MyDialogs.StandardButton.Cancel
        onAccepted: {
            console.log("save")
            if(page.title == "无标题"){
                fileDialog.open()
            }
            else{
//                funcWriteFile(textEdit.text, fileDialog.fileUrl)

//                textEdit.text = ""
//                page.title = "无标题"
            }
        }
        onDiscard: {
            console.log("Discard")
            page.title = "无标题"
            page.filePath = ""
            textEdit.text = ""

            fileDialogOpen.open()

        }
        onRejected: {
            console.log("Rejected")
        }
    }

    //对话框
    MyDialogs.FileDialog {
        id: fileDialog
        visible: false
        title: "另存为"
        folder: shortcuts.home
        selectExisting: false
        nameFilters: [ "文本文档 (*.txt)", "所有文件 (*.*)" ]
        onAccepted: {
            console.log("You chose: " + fileDialog.fileUrl)
            var path = String(fileDialog.fileUrl).split("/")
            page.title = path[path.length-1]
            page.filePath = fileDialog.fileUrl
            funcWriteFile(textEdit.text, fileDialog.fileUrl)

//            if(needOpen){
//               fileDialogOpen.open()
//            }
        }
        onRejected: {
//            if(needOpen){
//               fileDialogOpen.open()
//            }
        }
    }

    MyDialogs.FileDialog {
        id: fileDialogSave
        visible: false
        title: "另存为"
        folder: shortcuts.home
        selectExisting: false
        nameFilters: [ "文本文档 (*.txt)", "所有文件 (*.*)" ]
        onAccepted: {
            console.log("You chose: " + fileDialogSave.fileUrl)
            var path = String(fileDialogSave.fileUrl).split("/")
            page.title = path[path.length-1]
            page.filePath = fileDialogSave.fileUrl
            funcWriteFile(textEdit.text, fileDialogSave.fileUrl)
        }
        onRejected: {
        }
    }

    MyDialogs.FileDialog {
        id: fileDialogSaveAs
        visible: false
        title: "另存为"
        folder: shortcuts.home
        selectExisting: false
        nameFilters: [ "文本文档 (*.txt)", "所有文件 (*.*)" ]
        onAccepted: {
            console.log("You chose: " + fileDialogSaveAs.fileUrl)
            var path = String(fileDialogSaveAs.fileUrl).split("/")
            page.title = path[path.length-1]
            page.filePath = fileDialogSaveAs.fileUrl
            funcWriteFile(textEdit.text, fileDialogSaveAs.fileUrl)
        }
        onRejected: {
        }
    }

    MyDialogs.FileDialog {
        id: fileDialogOpen
        visible: false
        title: "打开"
        folder: shortcuts.home
        nameFilters: [ "文本文档 (*.txt)", "所有文件 (*.*)" ]
        onAccepted: {
            console.log("You chose: " + fileDialogOpen.fileUrl)
            var path = String(fileDialogOpen.fileUrl).split("/")
            page.title = path[path.length-1]
            page.filePath = fileDialogOpen.fileUrl
            textEdit.text = funcReadFile(fileDialogOpen.fileUrl)

            needOpen = false
        }
        onRejected: {
        }
    }

}
